import { createApp } from 'vue';
import App from './App.vue';
import routes from './router';

import { createRouter, createWebHashHistory } from 'vue-router';

let app = null;
let router = null;
let history = null;

// 将渲染操作放入 mount 函数
function mount() {
  history = createWebHashHistory();
  router = createRouter({
    history,
    routes,
  });

  app = createApp(App);
  app.use(router);
  app.mount('#micro-vite-vue3-demo');
  console.log('微应用child-vite渲染了');

  // fixBugForVueRouter4(router)
}

// 将卸载操作放入 unmount 函数
function unmount() {
  app.unmount();
  history.destroy();
  app = null;
  router = null;
  history = null;
  console.log('微应用child-vite卸载了');
}

// 微前端环境下，注册mount和unmount方法
console.log(window.__MICRO_APP_BASE_APPLICATION__);
if (window.__MICRO_APP_BASE_APPLICATION__) {
  window['appname-vite'] = { mount, unmount };
} else {
  // 非微前端环境直接渲染
  mount();
}
